package com.ruoyi.aero.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.aero.domain.StaffInfo;
import com.ruoyi.aero.domain.WorkshopInfo;
import com.ruoyi.aero.enums.Scale;
import com.ruoyi.aero.enums.TaskStatus;
import com.ruoyi.aero.service.IStaffInfoService;
import com.ruoyi.aero.service.IWorkshopInfoService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.aero.mapper.PlanInfoMapper;
import com.ruoyi.aero.domain.PlanInfo;
import com.ruoyi.aero.service.IPlanInfoService;
import org.springframework.util.ObjectUtils;

/**
 * 计划信息Service业务层处理
 * 
 * @author swb
 * @date 2024-06-04
 */
@Service
public class PlanInfoServiceImpl implements IPlanInfoService 
{
    @Autowired
    private PlanInfoMapper planInfoMapper;

    @Autowired
    private IStaffInfoService staffInfoService;

    @Autowired
    private IWorkshopInfoService workshopInfoService;

    /**
     * 查询计划信息
     * 
     * @param planId 计划信息主键
     * @return 计划信息
     */
    @Override
    public PlanInfo selectPlanInfoByPlanId(String planId)
    {
        PlanInfo planInfo = planInfoMapper.selectPlanInfoByPlanId(planId);
        updateStatus(planInfo);
        return planInfo;
    }

    /**
     * 查询计划信息列表
     * 
     * @param planInfo 计划信息
     * @return 计划信息
     */
    @Override
    public List<PlanInfo> selectPlanInfoList(PlanInfo planInfo)
    {
        List<PlanInfo> planInfoList = planInfoMapper.selectPlanInfoList(planInfo);
        for (PlanInfo plan : planInfoList) {
            updateStatus(plan);
        }

        return planInfoList;
    }

    /**
     * 新增计划信息
     * 
     * @param planInfo 计划信息
     * @return 结果
     */
    @Override
    public int insertPlanInfo(PlanInfo planInfo)
    {
        validDateTime(planInfo);
        repeatPlanName(planInfo);
        bindLeader(planInfo);
        planInfo.setPlanId(IdUtils.randomUUID());
        planInfo.setCreateTime(DateUtils.getNowDate());
        planInfo.setCreateBy(SecurityUtils.getUsername());
        return planInfoMapper.insertPlanInfo(planInfo);
    }

    /**
     * 修改计划信息
     * 
     * @param planInfo 计划信息
     * @return 结果
     */
    @Override
    public int updatePlanInfo(PlanInfo planInfo)
    {
        validDateTime(planInfo);
        repeatPlanName(planInfo);
        bindLeader(planInfo);
        planInfo.setUpdateTime(DateUtils.getNowDate());
        planInfo.setUpdateBy(SecurityUtils.getUsername());
        return planInfoMapper.updatePlanInfo(planInfo);
    }

    /**
     * 批量删除计划信息
     * 
     * @param planIds 需要删除的计划信息主键
     * @return 结果
     */
    @Override
    public int deletePlanInfoByPlanIds(String[] planIds)
    {
        return planInfoMapper.deletePlanInfoByPlanIds(planIds);
    }

    /**
     * 删除计划信息信息
     * 
     * @param planId 计划信息主键
     * @return 结果
     */
    @Override
    public int deletePlanInfoByPlanId(String planId)
    {
        return planInfoMapper.deletePlanInfoByPlanId(planId);
    }

    /**
     * 绑定负责人
     * @param planInfo
     */
    private void bindLeader(PlanInfo planInfo) {
        String planScale = planInfo.getPlanScale();
        if (Scale.GROUP_LEVEL.getScale().equals(planScale)) {
            throw new ServiceException("计划规模不能为班组级");
        } else if (Scale.WORKSHOP_LEVEL.getScale().equals(planScale)) {
            String workshopId = planInfo.getWorkshopId();
            if (ObjectUtils.isEmpty(workshopId)) {
                throw new ServiceException("车间级别的计划必须指定车间");
            }
            WorkshopInfo workshopInfo = workshopInfoService.selectWorkshopInfoByWorkshopId(workshopId);
            if (ObjectUtils.isEmpty(workshopInfo.getWorkshopLeaderId())) {
                // 对应的车间未指定负责人，暂不设置
                return;
            }
            planInfo.setPlanLeaderId(workshopInfo.getWorkshopLeaderId()); // 设置负责人为对应的车间负责人
        } else if (Scale.FACTORY_LEVEL.getScale().equals(planScale)) {
            StaffInfo staffInfo = staffInfoService.selectDirector();
            planInfo.setPlanLeaderId(staffInfo.getStaffId()); // 设置负责人为厂长
        } else {
            throw new ServiceException("计划规模只能是厂级或车间级");
        }
    }

    /**
     * 名称去重
     * @param planInfo
     */
    private void repeatPlanName(PlanInfo planInfo) {
        String planId = planInfo.getPlanId();
        String planName = planInfo.getPlanName();
        if (ObjectUtils.isEmpty(planName)) {
            throw new ServiceException("计划名称不能为空");
        }

        PlanInfo plan = planInfoMapper.selectPlanInfoByPlanName(planName);
        if (!ObjectUtils.isEmpty(plan)) {
            if (ObjectUtils.isEmpty(planId)) {
                // 如果是添加操作
                throw new ServiceException("计划名称与已有计划名称重复，添加失败");
            } else {
                // 如果是修改操作
                if (!planId.equals(plan.getPlanId())) {
                    throw new ServiceException("计划名称与已有计划名称重复，修改失败");
                }
            }
        }
    }

    /**
     * 判断起止时间是否合理
     * @param planInfo
     */
    private void validDateTime(PlanInfo planInfo) {
        Date startTime = planInfo.getStartTime();
        Date endTime = planInfo.getEndTime();
        if (startTime.after(endTime)) {
            throw new ServiceException("开始时间不能晚于结束时间");
        }
    }

    /**
     * 更新状态
     * @param planInfo
     */
    private void updateStatus(PlanInfo planInfo) {
        String planStatus = planInfo.getPlanStatus();
        if (TaskStatus.ON_GOING.getStatus().equals(planStatus)) {
            // 如果计划正在进行中
            Date startTime = planInfo.getStartTime();
            Date endTime = planInfo.getEndTime();

            if (DateUtils.getNowDate().before(startTime)) {
                planInfo.setPlanStatus(TaskStatus.NO_START.getStatus());
            } else if (DateUtils.getNowDate().after(endTime)) {
                planInfo.setPlanStatus(TaskStatus.FINISHED.getStatus());
            }
        }
    }
}
